package com.yxq.mdt.app.modules.socialcontact.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.socialcontact.bean.VideoTopicBean;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.CircleTopicAc;
import com.yxq.mdt.app.utils.MediaPlayerHelper;

import java.text.SimpleDateFormat;
import java.util.List;

import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;


public class MainSocialContactVideoAdapter extends BaseQuickAdapter<VideoTopicBean.ListBean, BaseViewHolder> {
    private String content = "#话题#";//话题
    private String contentTv = "";
    SpannableStringBuilder spannableStringBuilder;
    public CountDownTimer countDownTimer;
    public boolean isVoiceStart = false;

    public MainSocialContactVideoAdapter(int layoutResId, @Nullable List<VideoTopicBean.ListBean> data) {
        super(layoutResId, data);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
    }

    @Override
    protected void convert(BaseViewHolder helper, VideoTopicBean.ListBean item) {
        RecyclerView recyclerView = helper.getView(R.id.item_layout_content_recyclerView);
        JzvdStd jzvdStd = helper.getView(R.id.item_social_video_player);
        LinearLayout lint = helper.getView(R.id.item_social_lint);
        TextView textView = helper.getView(R.id.item_tv_subject);
        LinearLayout voiceTime = helper.getView(R.id.vice_lint_open);
        LinearLayout voice = helper.getView(R.id.publish_save_voice);
        if (item.getFileList() != null && item.getFileList().size() != 0) {
            contentTv = item.getContent();
            if (item.getFileList().get(0).getTopicId() != null && item.getContent() != null) {
                if (item.getContent().contains("#")) {
                    content = (item.getContent().substring(item.getContent().indexOf("#"), item.getContent().lastIndexOf("#") + 1));
                } else {
                    content = item.getContent();
                }
                spannableStringBuilder = new SpannableStringBuilder("");

                if (!TextUtils.isEmpty(contentTv)) {
                    spannableStringBuilder.append(contentTv);
                }
                if (item.getContent().contains("#")) {
                    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#ffa800"));
                    spannableStringBuilder.setSpan(foregroundColorSpan, 0, (item.getContent().substring(item.getContent().indexOf("#"), item.getContent().lastIndexOf("#") + 1)).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#222222")), (item.getContent().substring(item.getContent().indexOf("#"), item.getContent().lastIndexOf("#") + 1)).length(), spannableStringBuilder.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                    ClickableSpan clickableSpan = new ClickableSpan() {
                        @Override
                        public void onClick(@NonNull View view) {
                            Intent it = new Intent(mContext, CircleTopicAc.class);
                            it.putExtra("topicId", item.getFileList().get(helper.getItemViewType()).getTopicId());
                            it.putExtra("topic", (item.getContent().substring(item.getContent().indexOf("#"), item.getContent().lastIndexOf("#") + 1)));
                            mContext.startActivity(it);
                        }
                    };
                    //文本可点击，有点击事件
                    spannableStringBuilder.setSpan(clickableSpan, 0, content.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    // 设置此方法后，点击事件才能生效
                    textView.setMovementMethod(LinkMovementMethod.getInstance());
                    textView.setText(spannableStringBuilder);
                } else {
                    helper.setText(R.id.item_tv_subject, item.getContent());
                    helper.setTextColor(R.id.item_tv_subject, Color.parseColor("#222222"));
                    textView.setTextColor(Color.parseColor("#222222"));
                }
                //超过75个字符就显示全文按钮
                helper.setVisible(R.id.item_tv_subject_all, (textView.getText().length() > 75));
            }
            if (item.getIntroductionAudioFile() != null && item.getIntroductionAudioFile().size() != 0) {
                voiceTime.setVisibility(View.VISIBLE);
                if (item.getIntroductionAudioFile().get(0).getFileLength() < 1000) {
                    helper.setText(R.id.item_number_tv, item.getIntroductionAudioFile().get(0).getFileLength() + "″");
                } else {
                    helper.setText(R.id.item_number_tv, item.getIntroductionAudioFile().get(0).getFileLength() / 1000 + "″");
                }
                voiceTime.setOnClickListener(view -> {
                    if (isVoiceStart) {
                        isVoiceStart = false;
                        MediaPlayerHelper.realese();
                        countDownTimer.onFinish();
                    } else {
                        isVoiceStart = true;
                        MediaPlayerHelper.playSound(item.getIntroductionAudioFile().get(0).getFilePath(), mediaPlayer -> isVoiceStart = false);
                        if (item.getIntroductionAudioFile().get(0).getFileLength() <= 1000) {
                            getTime(item.getIntroductionAudioFile().get(0).getFileLength(), null, helper.getView(R.id.item_number_tv));
                        } else {
                            getTime(item.getIntroductionAudioFile().get(0).getFileLength(), null, helper.getView(R.id.item_number_tv));
                        }
                        countDownTimer.start();
                    }
                });
            } else {
                voiceTime.setVisibility(View.GONE);
            }
            //是否显示//是否为本人
            if (item.getRelationNum() != 0 || item.getRelationHideButton().equals("0")) {
                helper.setGone(R.id.item_layout_attention, false);
            } else {
                helper.setGone(R.id.item_layout_attention, true);
            }
            if (item.getGroupname() != null) {
                helper.setText(R.id.group_name, item.getGroupname());
                GlideHelper.setLoadRoundImage(mContext, item.getLogo(), R.mipmap.head_icon, helper.getView(R.id.item_layout_circle_img));
                helper.setGone(R.id.group_lint, true);
            } else {
                helper.setGone(R.id.group_lint, false);
            }
            helper.addOnClickListener(R.id.item_layout_attention);
            helper.addOnClickListener(R.id.group_lint);
            //头像
            GlideHelper.setLoadRoundImage(mContext, item.getHeadimg(), R.mipmap.head_icon, helper.getView(R.id.item_layout_nike_img));
            //昵称
            helper.setText(R.id.item_layout_nike_name, item.getGsName());
            //浏览数
            helper.setText(R.id.item_tv_count_browse, item.getViewCount() + "次浏览");
            //点赞数
            if (item.getUpCount() != 0) {
                helper.setText(R.id.item_layout_content_browse, item.getUpCount() + "");
            } else {
                helper.setText(R.id.item_layout_content_browse, "点赞");
            }
            //评论数
            if (item.getCommentCount() != 0) {
                helper.setText(R.id.item_layout_content_give_like, item.getCommentCount() + "");
            } else {
                helper.setText(R.id.item_layout_content_give_like, "评论");
            }
            //发布时间
            helper.setText(R.id.item_layout_time, TimeUtils.getFriendlyTimeSpanByNow(item.getCreateDate(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
            //性别
            if (item.getGiveLike() != 0) {
                helper.setImageResource(R.id.item_layout_content_browse_img, R.mipmap.zan);
            } else {
                helper.setImageResource(R.id.item_layout_content_browse_img, R.mipmap.icon_community_fabulous);
            }
            if (item.getPlace() != null) {
                helper.setText(R.id.item_layout_province, item.getPlace());
            } else {
                helper.setText(R.id.item_layout_province, "");
            }
            helper.setImageResource(R.id.item_iv_sex, item.getSex() == 1 ? R.mipmap.xb_nan : R.mipmap.xb_nv);
            helper.addOnClickListener(R.id.item_layout_circle_browse);
            helper.addOnClickListener(R.id.item_layout_nike_img);
            helper.addOnClickListener(R.id.item_layout_content_browse_img);
            helper.addOnClickListener(R.id.item_layout_content_browse_lint);
            helper.addOnClickListener(R.id.item_layout_content_share);
            switch (item.getFileList().get(0).getClass1()) {
                case "2":
                    lint.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                    voice.setVisibility(View.GONE);
                    ImageView imageViewVideo = jzvdStd.thumbImageView;
                    imageViewVideo.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    GlideHelper.setLoadRoundImage(mContext, item.getFileList().get(0).getFile(), R.color.gray, imageViewVideo);
                    jzvdStd.setUp(item.getFileList().get(0).getFile(), "", Jzvd.SCREEN_NORMAL);
                    Jzvd.setVideoImageDisplayType(JzvdStd.VIDEO_IMAGE_DISPLAY_TYPE_FILL_SCROP);
                    break;

            }

        }

    }

    /**
     * @param playTime  开始时间
     * @param imageView 获取当前控件
     * @param timeTv    获取显示秒数
     */
    public void getTime(int playTime, ImageView imageView, TextView timeTv) {
        if (imageView != null) {
            imageView.setImageResource(R.mipmap.voice_note_new);
        }
        countDownTimer = new CountDownTimer(playTime, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeTv.setText(millisUntilFinished / 1000 + "″");
            }

            @Override
            public void onFinish() {
                timeTv.setText(playTime / 1000 + "″");
                if (imageView != null) {
                    imageView.setImageResource(R.mipmap.voice_note);
                }
                countDownTimer.cancel();
            }
        };
    }
}
